<?php
namespace Admin\Model;
use Think\Model;
class AddressModel extends Model {
	protected $tableName = 'address'; 

	public function mselect2($where) {
		$order = C('ADDRESSORDER');

		$count      = $this->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		//进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $this->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

		return array(
			'list' => $list,
			'page' => $show,
		);
	}
	/**
	 * 列表
	 */
	public function mselect() {
		if(!empty($_POST['nickname'])) {
			$nickname = I('post.nickname','','strip_tags');
			$where['nickname'] = array('like','%'.$nickname.'%');
		}

		if(!empty($_POST['mobile'])) {
			$mobile = I('post.mobile','','strip_tags');
			$where['mobile'] = array('like','%'.$mobile.'%');
		}

		if(!empty($_POST['cs'])) {
			$cs = I('post.cs','','strip_tags');
			$where['cs'] = array('like','%'.$cs.'%');
		}

		if(!empty($_POST['qg'])) {
			$qg = I('post.qg','','strip_tags');
			$where['qg'] = array('like','%'.$qg.'%');
		}

		if(!empty($_POST['company'])) {
			$company = I('post.company','','strip_tags');
			$where['company'] = array('like','%'.$company.'%');
		}

		if(!empty($_POST['city'])) {
			$city = I('post.city','','strip_tags');
			$where['city'] = array('like','%'.$city.'%');
		}

		if(!empty($_POST['province'])) {
			$province = I('post.province','','strip_tags');
			$where['province'] = array('like','%'.$province.'%');
		}
		
		$order = C('ADDRESSORDER');

		$count      = $this->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		//进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $this->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

		return array(
			'list' => $list,
			'page' => $show,
		);
	}

	/**
	 * 添加修改
	 */
	public function msave() {
		if(isset($_POST['nickname'])) {
			$data['nickname'] = I('post.nickname','','strip_tags');
			$pings = Pinyin(trim(str_replace(array(1,2,3,4,5,6,7,8,9,0),'',I('post.nickname','','strip_tags'))));
			if(empty($pings)) {
				$data['ping'] = 'h';
			} else {
				$data['ping'] = $pings;
			}
		}
		if(isset($_POST['mobile'])){
			//$data['mobile'] = I('post.mobile','','strip_tags');
			$mobilearr = array_filter($_POST['mobile']);
			$data['mobile'] = json_encode($mobilearr);
		}
		if(isset($_POST['email'])) {
			$data['email'] = I('post.email','','strip_tags');
		}
		if(isset($_POST['weixinid'])) {
			$data['weixinid'] = I('post.weixinid','','strip_tags');
		}
		if(isset($_POST['qq'])) {
			$data['qq'] = I('post.qq','','strip_tags');
		}
		if(isset($_POST['cs'])) {
			$data['cs'] = I('post.cs','','strip_tags');
			$data['updatetime'] = time();
		}
		if(isset($_POST['qg'])) {
			$data['qg'] = I('post.qg','','strip_tags');
			$data['updatetime'] = time();
		}
		if(isset($_POST['company'])) {
			$data['company'] = I('post.company','','strip_tags');
		}
		if(isset($_POST['city'])) {
			$data['city'] = I('post.city','','strip_tags');
		}
		if(isset($_POST['province'])) {
			$data['province'] = I('post.province','','strip_tags');
		}
		if(isset($_POST['country'])) {
			$data['country'] = I('post.country','','strip_tags');
		}
		if(isset($_POST['content'])) {
			$data['content'] = I('post.content','','strip_tags');
		}
		if(isset($_POST['address'])) {
			$data['address'] = I('post.address','','strip_tags');
		}
		if(isset($_POST['headimgurl'])) {
			$data['headimgurl'] = I('post.headimgurl','','strip_tags');
		}
		$where['id'] = I('post.id','','intval');
		if(!empty($where['id'])) {
			$msg = $this->where($where)->save($data);
		} else {
			$data['time'] = time();
			$msg = $this->add($data);
		}
		return $msg;
	}

	public function msave2() {
		if(isset($_POST['nickname'])) {
			$data['nickname'] = I('post.nickname','','strip_tags');
			$pings = Pinyin(trim(str_replace(array(1,2,3,4,5,6,7,8,9,0),'',I('post.nickname','','strip_tags'))));
			if(empty($pings)) {
				$data['ping'] = 'h';
			} else {
				$data['ping'] = $pings;
			}
		}
		if(isset($_POST['mobile'])){
			$mobile = I('post.mobile','','strip_tags');
			$mobilearr = explode("|",$mobile);
			$data['mobile'] = json_encode($mobilearr);
		}

		if(!empty($_POST['photo'])) {
			$data['photo'] = I('post.photo','','strip_tags');
		}

		if(!empty($_POST['desc'])) {
			$data['desc'] = I('post.desc','','strip_tags');
		}

		if(isset($_POST['email'])) {
			$data['email'] = I('post.email','','strip_tags');
		}
		if(isset($_POST['weixinid'])) {
			$data['weixinid'] = I('post.weixinid','','strip_tags');
		}
		if(isset($_POST['qq'])) {
			$data['qq'] = I('post.qq','','strip_tags');
		}
		if(isset($_POST['cs'])) {
			$data['cs'] = I('post.cs','','strip_tags');
			$data['updatetime'] = time();
		}
		if(isset($_POST['qg'])) {
			$data['qg'] = I('post.qg','','strip_tags');
			$data['updatetime'] = time();
		}
		if(isset($_POST['company'])) {
			$data['company'] = I('post.company','','strip_tags');
		}
		if(isset($_POST['city'])) {
			$data['city'] = I('post.city','','strip_tags');
		}
		if(isset($_POST['province'])) {
			$data['province'] = I('post.province','','strip_tags');
		}
		if(isset($_POST['country'])) {
			$data['country'] = I('post.country','','strip_tags');
		}
		if(isset($_POST['content'])) {
			$data['content'] = I('post.content','','strip_tags');
		}
		if(isset($_POST['address'])) {
			$data['address'] = I('post.address','','strip_tags');
		}
		if(isset($_POST['headimgurl'])) {
			$data['headimgurl'] = I('post.headimgurl','','strip_tags');
		}
		$where['id'] = I('post.id','','intval');
		if(!empty($where['id'])) {
			$msg = $this->where($where)->save($data);
		} else {
			$data['time'] = time();
			$msg = $this->add($data);
		}
		return $msg;
	}
	
	/**
	 * 前台通讯录
	 */
	public function hselect($where=array(),$order='',$field="",$limit="") {
		//$year = date('Y');
		/*
		$year = 2016;
		$string = $year."0101";
		$yeartime = strtotime($string);

		$string = ($year+2)."0101";
		$nextyeartime = strtotime($string);
		$where['paytime'] = array('between',array($yeartime,$nextyeartime));
		 */
		$time = time();
	    $pretime = $time-(60*60*24*365);
		$where['paytime'] = ['gt',$pretime];

		$order = $order.'ping asc , id desc';
		if(!empty($field)) {
			$list = $this->field($field)->where($where)->limit($limit)->order($order)->select();
		} else {
			$list = $this->where($where)->order($order)->limit($limit)->select();
		}
		return $list;
	}

	public function hselectcount($where=array(),$order='',$field="") {
		//$year = date('Y');
		/*
		$year = 2016;
		$string = $year."0101";
		$yeartime = strtotime($string);

		$string = ($year+2)."0101";
		$nextyeartime = strtotime($string);
		$where['paytime'] = array('between',array($yeartime,$nextyeartime));
		 */
		$time = time();
	    $pretime = $time-(60*60*24*365);
		$where['paytime'] = ['gt',$pretime];

		$order = $order.'ping asc , id desc';
		if(!empty($field)) {
			$list = $this->field($field)->where($where)->order($order)->count();
		} else {
			$list = $this->where($where)->order($order)->count();
		}
		return $list;
	}
	
	/**
	 * 新加入同行
	 */
	public function hselectnow($where=array(),$order='') {
		$time = time();
		$start = $time-(3*24*60*60); // 3天
		$where['paytime'] = array('between',array($start,$time));
		$order = $order.'ping asc , id desc';
		$list = $this->where($where)->order($order)->select();
		return $list;
	}
}
